package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.oa.entity.VeOaCarInfoEntity;
import org.edu.modules.oa.entity.VeOaCarShiYongEntity;
import org.edu.modules.oa.mapper.IVeOaCarInfoMapper;
import org.edu.modules.oa.mapper.IVeOaCarShiYongMapper;
import org.edu.modules.oa.service.IVeOaCarShiYongService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
//继承iserviceimpl的方法之后可以调用导入数据到数据库的方法
public class VeOaCarShiYongServiceImp extends ServiceImpl<IVeOaCarShiYongMapper,VeOaCarShiYongEntity> implements IVeOaCarShiYongService {
    @Resource
    private IVeOaCarShiYongMapper veOaCarShiYongMapper;
    @Resource
    private IVeOaCarInfoMapper veOaCarInfoMapper;
    //加载表格
    public HashMap<String, Object> loadTable(Integer current, Integer pageSize, VeOaCarShiYongEntity veOaCarShiYongEntity) {
        HashMap<String, Object> map = new HashMap<>();
        IPage<VeOaCarShiYongEntity> shiYongPage = new Page<>(current, pageSize);//参数一是当前页，参数二是每页个数
        //创建查询的条件
        QueryWrapper<VeOaCarShiYongEntity> wrapper = new QueryWrapper<>();
        if(null != veOaCarShiYongEntity.getCarName() && !veOaCarShiYongEntity.getCarName().equals("")){
            wrapper.like("car_name", veOaCarShiYongEntity.getCarName());
        }
        if(null != veOaCarShiYongEntity.getYongcheUser() && !veOaCarShiYongEntity.getYongcheUser().equals("")){
            wrapper.like("yongche_user", veOaCarShiYongEntity.getYongcheUser());
        }
        if(null != veOaCarShiYongEntity.getYongcheDep() && !veOaCarShiYongEntity.getYongcheDep().equals("")){
            wrapper.like("yongche_dep", veOaCarShiYongEntity.getYongcheDep());
        }
        if(null != veOaCarShiYongEntity.getCarId() && !veOaCarShiYongEntity.equals("")){
            wrapper.eq("car_id", veOaCarShiYongEntity.getCarId());
        }
        wrapper.orderByDesc("create_time");
        shiYongPage = veOaCarShiYongMapper.selectPage(shiYongPage,wrapper);
        List<VeOaCarShiYongEntity> list = shiYongPage.getRecords();
        int count = veOaCarShiYongMapper.selectCount(wrapper);
        map.put("list",list);
        map.put("count",count);
        return map;
    }

    //删除一条记录
    public int deleteEmplotyById(Integer id) {
        Integer res = veOaCarShiYongMapper.deleteById(id);
        return res;
    }

   //修改用户信息的方法
    public int setEmplotyById(VeOaCarShiYongEntity veOaCarShiYongEntity) {
        int res = veOaCarShiYongMapper.updateById(veOaCarShiYongEntity);
        return res;
    }

    //查看全部的车辆
    public List<VeOaCarInfoEntity> selectCarAll() {
        List<VeOaCarInfoEntity> list = veOaCarInfoMapper.selectList(null);
        return list;
    }

    //添加一条车辆使用记录
    public int addEmploty(VeOaCarShiYongEntity veOaCarShiYongEntity) {
        int res = veOaCarShiYongMapper.insert(veOaCarShiYongEntity);
        return res;
    }

    //查看全部的使用记录导出用
    public List<VeOaCarShiYongEntity> selectAllEmploty() {
        return veOaCarShiYongMapper.selectList(null);
    }

    //查看车辆是否存在
    public VeOaCarInfoEntity selectCarByName(String carName) {
        QueryWrapper<VeOaCarInfoEntity> wrapper = new QueryWrapper();
        wrapper.eq("car_name", carName);
        return veOaCarInfoMapper.selectOne(wrapper);
    }

    //高级查询
    public HashMap<String, Object> advancedQuery(String conditionStr) {
       List<VeOaCarShiYongEntity> list =  veOaCarShiYongMapper.selectShiYongList(conditionStr);
       int count = veOaCarShiYongMapper.selectShiYongCount(conditionStr);
       HashMap<String,Object> map = new HashMap<>();
       map.put("count",count);
       map.put("list",list);
       return map;
    }

    //查看当前车辆的全部使用记录
    public List<VeOaCarShiYongEntity> exportEmplotyById(Integer carId) {
        QueryWrapper<VeOaCarShiYongEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("car_id",carId);
        return veOaCarShiYongMapper.selectList(wrapper);
    }


}
